package com.aigou.admin.dao.impl;

import com.aigou.admin.dao.BrandDAO;
import com.aigou.admin.entity.Brand;
import com.aigou.admin.util.JDBCUtils;

import java.util.List;

public class BrandDAOImpl implements BrandDAO {
    @Override
    public Integer selectBrandCount() {
        return JDBCUtils.executeCount("select count(*) from s_brand");
    }

    @Override
    public List<Brand> selectBrandlimit(int start, int rows) {
        return JDBCUtils.executeQuery(Brand.class,"select * from s_brand limit ?,?",
                start,rows);
    }

    @Override
    public Brand selectBybrandName(String brandName) {
        List<Brand> brands = JDBCUtils.executeQuery(Brand.class,"select * from s_brand where brand_name=?",brandName);
        if (brands.isEmpty()){
            return null;
        }
        return brands.get(0);
    }

    @Override
    public List<Brand> selectAll() {
        List<Brand> brands = JDBCUtils.executeQuery(Brand.class,"select * from s_brand");
        return brands;
    }

    @Override
    public Brand selectById(String brandId) {
        List<Brand> brands = JDBCUtils.executeQuery(Brand.class,"select * from s_brand where id=?",brandId);
        if (brands.isEmpty()){
            return null;
        }
        return brands.get(0);
    }

    @Override
    public boolean insert(Brand brand) {
        int rows = JDBCUtils.executeUpdate("insert into s_brand(id,brand_name,brand_type,brand_img) values(?,?,?,?)",
                brand.getId(),brand.getBrand_name(),brand.getBrand_type(),brand.getBrand_img());
        return rows > 0;
    }

    @Override
    public boolean update(Brand brand) {
        int rows = JDBCUtils.executeUpdate("update s_brand set brand_name=?,brand_type=?,brand_img=? where id=?",
                brand.getBrand_name(),brand.getBrand_type(),brand.getBrand_img(),brand.getId());
        return rows > 0;
    }

    @Override
    public boolean delete(String brandId) {
        int rows = JDBCUtils.executeUpdate("delete from s_brand where id=?",brandId);
        return rows > 0;
    }
}
